package com.ecourseapp.NativeForRN.Alipay;

import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.ecourseapp.NativeForRN.Alipay.util.OrderInfoUtil2_0;
import com.ecourseapp.NativeForRN.Alipay.util.PayResult;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;


import java.util.Map;

public class AlipayModule extends ReactContextBaseJavaModule {

    public AlipayModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "Alipay";
    }

    // 获取应用包名
    // 要导出一个方法给JavaScript使用，Java方法需要使用注解@ReactMethod
    @ReactMethod
    public void showHello() {
        String name = "hello alipay";
        Toast.makeText(getReactApplicationContext(), name, Toast.LENGTH_LONG).show();
    }


    /**
     * 支付宝支付业务：入参app_id
     */
    public static final String APPID = "2017112800229202";
    public static final String RSA2_PRIVATE = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC+r34oUl10909I3kbD3c8Q0xu5E0b026IMcwyLmpZhBPY1WtpIPWm7lIdhujy+JDMZtT+O+NDVqdiPfEv4yDBZFUs9LHQWYxBkG/BvZk6sED7+EeTjzhg7CqZcurKtV9Te5uHfh/KYx9OQIIytcEy5O/dgbpXVekYao8z153LKs9ykuhYwaQfNo2OPHmWrK9CGPoxMXa6qL/5WIXvRYcvaBqI/nsS5YipIq2ZG3oMIHnAZrgaAP/fPMSP8I6flbaDqaSLd5h/VYLMzg1mNThE9nbTODYeB/vtY89eCU3edKt5AuIBPE6f+lmf3Ic6vyjlw6jg8uylEf2mUTe9Iv9+XAgMBAAECggEAUSd3VcnYJJOslqKjE3EvU0XForrllAh1qz67tB6Sqa/m6UZFaEFa4o6QH7OXboURinttFXgnyQeEN2z7GGJ60S8S2nI05eI7c5/AcGjmSqyPYqpS8/EetuINc6Gxsby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oQ==";
    public static final String RSA_PRIVATE = "";

    private static final int SDK_PAY_FLAG = 1;

    @ReactMethod
    public void payV2(final String orderInfo, final Promise promise) {

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask(getCurrentActivity());
                Map<String, String> result = alipay.payV2(orderInfo, true);
                Log.i("msp", result.toString());

                PayResult payResult = new PayResult(result);
                promise.resolve(payResult.getResultStatus());
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    @ReactMethod
    public void pay(final String orderId, final Callback successBack, final Callback errorBack) {

        if (TextUtils.isEmpty(APPID) || (TextUtils.isEmpty(RSA2_PRIVATE) && TextUtils.isEmpty(RSA_PRIVATE))) {
            Toast.makeText(getReactApplicationContext(), "APPID或RSA未设置", Toast.LENGTH_LONG).show();
            return;
        }

        boolean rsa2 = (RSA2_PRIVATE.length() > 0);
        Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, rsa2);


        String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

        String privateKey = rsa2 ? RSA2_PRIVATE : RSA_PRIVATE;
        String sign = OrderInfoUtil2_0.getSign(params, privateKey, rsa2);
        final String orderInfo = orderParam + "&" + sign;

        try {
            Runnable payRunnable = new Runnable() {
                @Override
                public void run() {
                    PayTask alipay = new PayTask(getCurrentActivity());
                    Map<String, String> result = alipay.payV2(orderInfo, true);
                    Log.i("msp", result.toString());

                    PayResult payResult = new PayResult(result);
                    successBack.invoke(payResult.getResultStatus());
                }
            };
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        } catch (Exception e) {
            errorBack.invoke(e.getMessage());
        }
    }


}